package com.jepusi.exspringboot;

import org.apache.poi.ss.usermodel.*;


import java.io.*;


public class ExSpringbootApplication {

    public static void main(String[] args) {
        try {
            // 指定目录路径
            File directoryPath = new File("D:\\brxx\\ex");
            BufferedWriter writer = new BufferedWriter(new FileWriter(directoryPath+"\\file.txt"));
            // 列出该目录下的所有文件
            File[] filesList = directoryPath.listFiles();

            if (filesList != null) {
                for (File file : filesList) {
                    if (file.isFile() && file.getName().endsWith(".xlsx")) {
                        // 读取 Excel 文件
                        FileInputStream inputStream = new FileInputStream(file);
                        Workbook workbook = WorkbookFactory.create(inputStream);

                        // 获取第一个工作表
                        Sheet sheet = workbook.getSheetAt(0);
                        int i = 0;
                        // 遍历工作表中的行和列
                        // ArrayList<Transaction> list = new ArrayList<>();
                        String zhanghao = "";
                        String huming = "";
                        String kahao = "";
                        String huobi = "";
                        for (Row row : sheet) {
                            i++;
                            if(i==2){
                                for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
                                    if(row.getCell(j).toString().equals("账号")){
                                        zhanghao = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("户名")){
                                        huming = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("卡号")){
                                        kahao = row.getCell(j+1).toString();
                                    }
                                    if(row.getCell(j).toString().equals("货币")){
                                        huobi = row.getCell(j+1).toString();
                                    }
                                }
                            }
                            if (i >= 4) {
                                StringBuilder builder = new StringBuilder();
                                builder.append(zhanghao+",");
                                builder.append(huming+",");
                                builder.append(kahao+",");
                                builder.append(huobi+",");
                                for (Cell cell : row) {
                                    builder.append(cell.toString() + ",");
                                }

                                String builderResult = builder
                                        .deleteCharAt(builder.toString().length()-1).toString();
                                System.out.println(builderResult);
                                writer.write(builderResult);
                                writer.newLine();
                            }
                        }
                        // 关闭流和工作簿
                        workbook.close();
                        inputStream.close();
                    }
                }
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
